Mapping & Geocoding#

https://melaniewalsh.github.io/Intro-Cultural-Analytics/07-Mapping/01-Mapping.html

Making Interactive Maps#

To map our geocoded coordinates, we’re going to use the Python library Folium. Folium is built on top of the popular JavaScript library Leaflet.

To install and import Folium, run the cells below:

!pip install folium
Collecting folium
  Downloading folium-0.14.0-py2.py3-none-any.whl (102 kB)
?25l     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 0.0/102.3 kB ? eta -:--:--
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 102.3/102.3 kB 2.8 MB/s eta 0:00:00
?25h
Collecting branca>=0.6.0 (from folium)
  Downloading branca-0.6.0-py3-none-any.whl (24 kB)
Requirement already satisfied: jinja2>=2.9 in /opt/hostedtoolcache/Python/3.10.11/x64/lib/python3.10/site-packages (from folium) (3.1.2)
Collecting numpy (from folium)
  Downloading numpy-1.24.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (17.3 MB)
?25l     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 0.0/17.3 MB ? eta -:--:--
     ╸━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 0.3/17.3 MB 9.2 MB/s eta 0:00:02
     ━╸━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 0.7/17.3 MB 10.5 MB/s eta 0:00:02
     ━━╸━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.3/17.3 MB 12.2 MB/s eta 0:00:02
     ━━━━╺━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.9/17.3 MB 13.8 MB/s eta 0:00:02
     ━━━━━━╸━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.9/17.3 MB 16.4 MB/s eta 0:00:01
     ━━━━━━━━━╸━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 4.2/17.3 MB 20.0 MB/s eta 0:00:01
     ━━━━━━━━━━━━━╸━━━━━━━━━━━━━━━━━━━━━━━━━━ 6.0/17.3 MB 24.4 MB/s eta 0:00:01
     ━━━━━━━━━━━━━━━━━━━╸━━━━━━━━━━━━━━━━━━━━ 8.5/17.3 MB 30.2 MB/s eta 0:00:01
     ━━━━━━━━━━━━━━━━━━━━━━━━━━╸━━━━━━━━━━━━━ 11.6/17.3 MB 49.9 MB/s eta 0:00:01
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╺━━ 16.2/17.3 MB 95.1 MB/s eta 0:00:01
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╸ 17.3/17.3 MB 104.7 MB/s eta 0:00:01
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 17.3/17.3 MB 69.9 MB/s eta 0:00:00
?25h
Requirement already satisfied: requests in /opt/hostedtoolcache/Python/3.10.11/x64/lib/python3.10/site-packages (from folium) (2.30.0)
Requirement already satisfied: MarkupSafe>=2.0 in /opt/hostedtoolcache/Python/3.10.11/x64/lib/python3.10/site-packages (from jinja2>=2.9->folium) (2.1.2)
Requirement already satisfied: charset-normalizer<4,>=2 in /opt/hostedtoolcache/Python/3.10.11/x64/lib/python3.10/site-packages (from requests->folium) (3.1.0)
Requirement already satisfied: idna<4,>=2.5 in /opt/hostedtoolcache/Python/3.10.11/x64/lib/python3.10/site-packages (from requests->folium) (3.4)
Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/hostedtoolcache/Python/3.10.11/x64/lib/python3.10/site-packages (from requests->folium) (2.0.2)
Requirement already satisfied: certifi>=2017.4.17 in /opt/hostedtoolcache/Python/3.10.11/x64/lib/python3.10/site-packages (from requests->folium) (2023.5.7)
Installing collected packages: numpy, branca, folium
Successfully installed branca-0.6.0 folium-0.14.0 numpy-1.24.3
[notice] A new release of pip is available: 23.0.1 -> 23.1.2
[notice] To update, run: pip install --upgrade pip
import folium

Base Map#

First, we need to establish a base map. This is where we’ll map our geocoded Ithaca locations. To do so, we’re going to call folium.Map()and enter the general latitude/longitude coordinates of the Ithaca area at a particular zoom.

(To find latitude/longitude coordintes for a particular location, you can use Google Maps, as described here.)

CCDR_map = folium.Map(location=[20,0], zoom_start=2.5)
CCDR_map
Make this Notebook Trusted to load map: File -> Trust Notebook

Add a Marker#

Adding a marker to a map is easy with Folium! We’ll simply call folium.Marker() at a particular lat/lon, enter some text to display when the marker is clicked on, and then add it to our base map.

folium.Marker(location=[28.3949,84.1240], popup="Nepal").add_to(CCDR_map)
CCDR_map
Make this Notebook Trusted to load map: File -> Trust Notebook

Choropleth Maps#

To create a chropleth map with Folium, we need to pair a “geo.json” file (which indicates which parts of the map to shade) with a CSV file (which includes the variable that we want to shade by).

import pandas as pd
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[5], line 1
----> 1 import pandas as pd

ModuleNotFoundError: No module named 'pandas'
world_boundaries = (

    "http://geojson.xyz/naturalearth-3.3.0/ne_50m_admin_0_countries.geojson"

)
CCDR_map = folium.Map(location=(20, 0), zoom_start=3, tiles="cartodb positron")
folium.GeoJson(world_boundaries).add_to(CCDR_map)
CCDR_map
Make this Notebook Trusted to load map: File -> Trust Notebook
CCDR_countries = pd.read_csv("../_static/CCDR_countries.csv")
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[8], line 1
----> 1 CCDR_countries = pd.read_csv("../_static/CCDR_countries.csv")

NameError: name 'pd' is not defined
CCDR_countries
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[9], line 1
----> 1 CCDR_countries

NameError: name 'CCDR_countries' is not defined
CCDR_map = folium.Map(location=[20,0], zoom_start=2.5)

folium.Choropleth(
    geo_data = world_boundaries,
    name = "Countries covered by CCDR-tools analytics",
    data = CCDR_countries,
    columns = ['ISO3','Status'],
    key_on = 'feature.properties.iso_a3',
    bins = [0, 1, 2, 3],
    fill_color = 'RdYlGn',
    fill_opacity = 0.7,
    line_opacity = 0.2,
    nan_fill_color = "none",
    legend_name = 'Countries covered'
).add_to(CCDR_map)

tooltip = folium.features.GeoJson(
    world_boundaries,
    tooltip=folium.features.GeoJsonTooltip(fields=['name', 'pop_est', 'economy', 'income_grp'],
                                           aliases=['Country', 'Population', 'Economy', 'Income'],
                                           localize=True)
                                )
CCDR_map.add_child(tooltip)


folium.LayerControl().add_to(CCDR_map)

CCDR_map
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[10], line 6
      1 CCDR_map = folium.Map(location=[20,0], zoom_start=2.5)
      3 folium.Choropleth(
      4     geo_data = world_boundaries,
      5     name = "Countries covered by CCDR-tools analytics",
----> 6     data = CCDR_countries,
      7     columns = ['ISO3','Status'],
      8     key_on = 'feature.properties.iso_a3',
      9     bins = [0, 1, 2, 3],
     10     fill_color = 'RdYlGn',
     11     fill_opacity = 0.7,
     12     line_opacity = 0.2,
     13     nan_fill_color = "none",
     14     legend_name = 'Countries covered'
     15 ).add_to(CCDR_map)
     17 tooltip = folium.features.GeoJson(
     18     world_boundaries,
     19     tooltip=folium.features.GeoJsonTooltip(fields=['name', 'pop_est', 'economy', 'income_grp'],
     20                                            aliases=['Country', 'Population', 'Economy', 'Income'],
     21                                            localize=True)
     22                                 )
     23 CCDR_map.add_child(tooltip)

NameError: name 'CCDR_countries' is not defined